package com.glory.gloryquickui.view

import android.content.Context
import android.util.DisplayMetrics
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.core.view.children
import com.glory.gloryquickui.R
import com.glory.gloryquickui.entity.FieldBean
import com.glory.gloryquickui.entity.TableCustomBean
import com.glory.gloryquickui.widget.lock_view.locktableview.DisplayUtil
import com.glory.gloryquickui.widget.lock_view.locktableview.LockTableView
import com.glory.gloryquickui.widget.lock_view.xrecyclerview.ProgressStyle

/**
 *Created:ZY
 *创建时间：2021/4/12
 *Describe:
 */
class TableViewController(context: Context?) : ViewController<FieldBean?>(context) {

    private var tableView:LockTableView? = null
    var allTablelineData:ArrayList<ArrayList<TableCustomBean>> = ArrayList()
    var LockFristColumn = false

    override fun resLayoutId(): Int {
        return R.layout.layout_tableview
    }

    override fun onCreatedView(view: View?) {
        initDisplayOpinion()
    }

    override fun onBindView(data: FieldBean?) {
        val titleData = ArrayList<TableCustomBean>()
        data!!.config.children.forEach { item ->
            var bean = TableCustomBean()
            bean.textName = item.label
            bean.isHeadSort = item.sortable
            titleData.add(bean)
        }
        allTablelineData.add(titleData)
        data?.tablelineData?.let { allTablelineData.addAll(it) }

        var minColumnWidth = 0
        if (titleData.size < 5) {
            minColumnWidth = (DisplayUtil.screenWidthDip/titleData.size).toInt() - 30
        }else{
            minColumnWidth = (DisplayUtil.screenWidthDip/4).toInt() - 30
        }
        data.tablelineData?.sortBy {
            it[0].textName
        }

        tableView = LockTableView(context  , view as ViewGroup?,allTablelineData)
        tableView!!.setLockFristColumn(LockFristColumn) //是否锁定第一列
            .setLockFristRow(false) //是否锁定第一行
            .setMaxColumnWidth(100) //列最大宽度
            .setMinColumnWidth(minColumnWidth) //列最小宽度
            //.setColumnWidth(1,60) //设置指定列文本宽度(从0开始计算,宽度单位dp)
            .setMinRowHeight(20)//行最小高度
            .setMaxRowHeight(60)//行最大高度
            .setTextViewSize(16) //单元格字体大小
            .setCellPadding(15)//设置单元格内边距(dp)
            .setFristRowBackGroudColor(R.color.table_head)//表头背景色
            .setTableHeadTextColor(R.color.theme_blue)//表头字体颜色
            .setTableContentTextColor(R.color.border_color)//单元格字体颜色
            .setNullableString("N/A") //空值替换值
            .setBorder(true)
            .setStripe(true)
            .setOnHeadItemClickListenter{view,postion ->
                var indexPostion = postion
                if(LockFristColumn) {
                    if (indexPostion == -1) {
                        indexPostion = 0
                    } else {
                        indexPostion += 1
                    }
                }
                val dd = view as LinearLayout
                val ff = dd.childCount
                val sdd = dd.children
                titleData.forEachIndexed { index, tableCustomBean ->
                    if(index != indexPostion){
                        tableCustomBean.sort = ""
                    }
                }
                dd.children.iterator().forEach {
                    view ->
                    if(view is ImageView){
                        val imageView = view as ImageView
                        if(titleData.get(indexPostion)?.sort == "") {
                            titleData.get(indexPostion)?.sort = "asc"
                            data.tablelineData?.sortByDescending {
                                it[indexPostion].textName
                            }
                            allTablelineData.clear()
                            allTablelineData.add(titleData)
                            data?.tablelineData?.let { allTablelineData.addAll(it) }
                            tableView?.setTableDatas(allTablelineData)
                        }else if (titleData.get(indexPostion)?.sort == "asc") {
                            titleData.get(indexPostion)?.sort = "desc"
                            data.tablelineData?.sortBy {
                                it[indexPostion].textName
                            }
                            allTablelineData.clear()
                            allTablelineData.add(titleData)
                            data?.tablelineData?.let { allTablelineData.addAll(it) }
                            tableView?.setTableDatas(allTablelineData)
                        }else if (titleData.get(indexPostion)?.sort == "desc") {
                            titleData.get(indexPostion)?.sort = "asc"
                            data.tablelineData?.sortByDescending {
                                it[indexPostion].textName
                            }
                            allTablelineData.clear()
                            allTablelineData.add(titleData)
                            data?.tablelineData?.let { allTablelineData.addAll(it) }
                            tableView?.setTableDatas(allTablelineData)
                        }
                    }
                }
            }
            .show()
        tableView?.tableScrollView?.setPullRefreshEnabled(false)
        tableView?.tableScrollView?.setLoadingMoreEnabled(false)
        tableView?.tableScrollView?.setRefreshProgressStyle(ProgressStyle.SquareSpin);
    }

    private fun initDisplayOpinion() {
        val dm: DisplayMetrics = context!!.resources!!.displayMetrics
        DisplayUtil.density = dm.density
        DisplayUtil.densityDPI = dm.densityDpi
        DisplayUtil.screenWidthPx = dm.widthPixels
        DisplayUtil.screenhightPx = dm.heightPixels
        DisplayUtil.screenWidthDip = DisplayUtil.px2dip(context, dm.widthPixels.toFloat()).toFloat()
        DisplayUtil.screenHightDip = DisplayUtil.px2dip(context, dm.heightPixels.toFloat()).toFloat()
    }

    override fun masterPart(): View? {
        return null
    }

}